﻿/*****************************************************************
 *@Copyright all rights reserved © 2014 Yuga
 *@author Oplink OMS
 *Contact: zengkang007@gmail.com
 *****************************************************************/
package com.oplink.webbase.modules.sys.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oplink.webbase.common.persistence.Page;
import com.oplink.webbase.modules.sys.dao.RoleMenuDao;
import com.oplink.webbase.modules.sys.entity.RoleMenu;

/**
 * 告警Service
 * 
 * @author Oplink OMS
 * @version 2014-09-02
 */
@Component
@Transactional
public class RoleMenuService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(RoleMenuService.class);

	@Autowired
	private RoleMenuDao roleMenuDao;

	public RoleMenu get(Long id) {
		return roleMenuDao.findOne(id);
	}

	public Page<RoleMenu> find(Page<RoleMenu> page, RoleMenu sysRoleMenu) {
		DetachedCriteria dc = roleMenuDao.createDetachedCriteria();
		// 添加查询条件
		if ((null != sysRoleMenu.getMenuId()) && StringUtils.isNotEmpty(sysRoleMenu.getMenuId().toString())) {
			dc.add(Restrictions.eq("menuId", sysRoleMenu.getMenuId()));
		}
		dc.add(Restrictions.eq(RoleMenu.DEL_FLAG, RoleMenu.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return roleMenuDao.find(page, dc);
	}

	/**
	 * @function 创建查询
	 **/
	public Page<RoleMenu> getUserRoleMenu(Page<RoleMenu> page, RoleMenu roleMenu) {
		DetachedCriteria dc = roleMenuDao.createDetachedCriteria();
		// 添加查询条件
		if ((null != roleMenu.getMenuId()) && StringUtils.isNotEmpty(roleMenu.getMenuId().toString())) {
			dc.add(Restrictions.like("menuId", "%" + roleMenu.getMenuId() + "%"));
		}
		dc.add(Restrictions.eq(RoleMenu.DEL_FLAG, RoleMenu.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return roleMenuDao.find(page, dc);
	}

	public RoleMenu getRoleMenu(Long id) {
		return roleMenuDao.findOne(id);
	}

	public void saveRoleMenu(RoleMenu entity) {
		roleMenuDao.save(entity);
	}

	public void deleteRoleMenu(Long id) {
		roleMenuDao.delete(id);
	}

	@Autowired
	public void setRoleMenuDao(RoleMenuDao roleMenuDao) {
		this.roleMenuDao = roleMenuDao;
	}
}
